package uni.UNIBEE7B8D.CommonTool;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

/**
 * Created by sdtz on 2018/7/4.
 */
@SuppressLint("AppCompatCustomView")
public class DrawableEditText extends EditText {


    public DrawableEditText(Context context) {
        super(context);
    }

    public DrawableEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawableEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public DrawableEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 触摸状态
//        if (event.getAction() == MotionEvent.ACTION_UP) {
//            // 监听DrawableLeft
//            if (onDrawableLeftListener != null) {
//                // 判断DrawableLeft是否被点击
//                Drawable drawableLeft = getCompoundDrawables()[0];
//                // 当按下的位置 < 在EditText的到左边间距+图标的宽度+Padding
//                // 在EditText的到右边间距-图标的宽度-Padding
//                if (drawableLeft != null && event.getRawX()
//                if (drawableRight != null && event.getRawX() >= (getRight() - getTotalPaddingRight() - drawableRight.getBounds().width())) {
//                    // 执行DrawableRight点击事件
//                    onDrawableRightListener.onDrawableRightClick();
//                }
//            }
//        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
//                if (mRightListener != null) {
//                    Drawable drawableRight = getCompoundDrawables()[DRAWABLE_RIGHT] ;
//                    if (drawableRight != null && event.getRawX() >= (getRight() - drawableRight.getBounds().width())) {
//                        mRightListener.onDrawableRightClick(this) ;
//                        return true ;
//                    }
//                }

                if (onDrawableLeftListener != null) {
                    Drawable drawableLeft = getCompoundDrawables()[0] ;
                    if (drawableLeft != null && event.getRawX() <= (getLeft() + drawableLeft.getBounds().width())) {
                        onDrawableLeftListener.onDrawableLeftClick();
                        return true ;
                    }
                }
                break;
        }

        return super.onTouchEvent(event);
    }

    // 定义一个DrawableLeft点击事件接口
    public interface OnDrawableLeftListener {
        void onDrawableLeftClick();
    }

    private OnDrawableLeftListener onDrawableLeftListener;

    public void setOnDrawableLeftListener(OnDrawableLeftListener onDrawableLeftListener) {
        this.onDrawableLeftListener = onDrawableLeftListener;
    }

    // 定义一个DrawableRight点击事件接口
    public interface OnDrawableRightListener {
        void onDrawableRightClick();
    }

    private OnDrawableRightListener onDrawableRightListener;

    public void setOnDrawableRightListener(OnDrawableRightListener onDrawableRightListener) {
        this.onDrawableRightListener = onDrawableRightListener;
    }
}
